package utils

import "strings"

const (
	LeftAlign   uint8 = iota // 左对齐
	CenterAlign              // 居中对齐
	RightAlign               // 右对齐
)

// GetHavePlaceholderStr 获取指定位数占位符的字符串
func GetHavePlaceholderStr(handelType uint8, content string, size int, placeholder ...byte) string {
	l := len([]rune(content))
	if l >= size {
		return content
	}

	defaultPlaceholder := byte(' ')
	if len(placeholder) > 0 {
		defaultPlaceholder = placeholder[0]
	}

	buf := new(strings.Builder)
	needAppendSize := size - l
	waitAppendBytes := make([]byte, 0, needAppendSize)
	switch handelType {
	case LeftAlign:
		buf.WriteString(content)
		for i := 0; i < needAppendSize; i++ {
			waitAppendBytes = append(waitAppendBytes, defaultPlaceholder)
		}
		buf.Write(waitAppendBytes)
	case CenterAlign:
		for i := 0; i < needAppendSize/2; i++ {
			waitAppendBytes = append(waitAppendBytes, defaultPlaceholder)
		}
		buf.Write(waitAppendBytes)
		buf.WriteString(content)
		buf.Write(waitAppendBytes)
	case RightAlign:
		for i := 0; i < needAppendSize; i++ {
			waitAppendBytes = append(waitAppendBytes, defaultPlaceholder)
		}
		buf.Write(waitAppendBytes)
		buf.WriteString(content)
	}
	return buf.String()
}
